
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value1: "",
    value2:0,
    tabList: [
      {
        text:'全部',
        value:"",
      },
      {
        text: '未走访',
        value:'未走访',
      },
      {
        text: '无需求',
        value:'已走访无需求',
      },
      {
        text: '有意向',
        value:'已走访有意向',
      },
      {
        text: '已授信',
        value:'已走访且授信',
      },
      {
        text: '已用信',
        value:'已走访且用信',
      },
      {
        text: '不准入',
        value:'有意向未准入',
      },
    ],
    option2:[    {
      text:'全部11',
      value:0,
    },],
    userId: '',
    serachValue:"",
    showList: true,
    list: [],
    pageNum:1,
    selectList:[
      {
        title:'走访时间',
        key:1,
        list:[
          {title:'不限',value:'走访时间',key:1,active:true},
          {title:'近7天',value:'7',key:1,active:false},
          {title:'近30天',value:'30',key:1,active:false},
          {title:'近60天',value:'60',key:1,active:false},
          {title:'近90天',value:'90',key:1,active:false},
          {title:'近半年',value:'180',key:1,active:false},
          {title:'近一天',value:'360',key:1,active:false},
        ]
      },
      {
        title:'关注状态',
        key:2,
        list:[
          {title:'不限',value:'关注状态',key:2,active:true},
          {title:'已关注',value:'1',key:2,active:false},
          {title:'未关注',value:'0',key:2,active:false},
        ]
      },
      {
        title:'任务类型',
        key:3,
        list:[
          {title:'不限',value:'任务类型', key:3,active:true},
          {title:'主动认领',value:'claim', key:3,active:false},
          {title:'自动分配',value:'assign', key:3,active:false},
        ]
      },
    ],
    activeList:[
      '走访时间',
      '关注状态',
      '任务类型'
    ],
    visitDay:'',
    visitType:"",
    isSubscribed:'',
  },
  async getList () {
    const userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    const {serachValue,value1,visitDay,visitType,isSubscribed}=this.data;
    const params = {
      pageNum: 1,
      pageSize: 25,
      corpName:serachValue,
     //userId: "b7e4448a626a41bea5666e0aefbbe52a",
     userId:userInfo.userId,
      visitType:visitType, //主动分配
      visitDay:visitDay ,//天数
      visitStatus:value1,//未走访
      isSubscribed:isSubscribed
    }
    console.log({ ...params }, 'params');
    const res = await getApp().globalData.request(
      "/visit/getListV2",
      { ...params },
      "POST"
    );
    console.log(res.data)
    if (res.data?.list.length > 0) {
      this.setData({
        pageNum: ++this.data.pageNum,
        list: this.data.list.concat(res.data.list)
      })
    }
  },

  selectTab(e){
    console.log(e);
    // indexs： 0：走访时间  1：关注状态  2：任务类型
    let {value,indexs,index}=e.currentTarget.dataset;
    let arrA=this.data.selectList;
    arrA[indexs].list.map((item=>{
      if(item!=value){
        item.active=false
      }
    }))
    arrA[indexs].list[index].active=true;
    this.setData({
       selectList:[...arrA],
       visitDay:indexs==0?(value=='走访时间'?"":value):this.data.visitDay,
       visitType:indexs==2?(value=='任务类型'?"":value):this.data.visitType,
       isSubscribed:indexs==1?(value=='关注状态'?"":value):this.data.isSubscribed,
       list:[],
       pageNum:1,
    }) ;
     this.getList();


  },

  //搜索企业名称
  //回车进行搜索
 searchName(e){
  //  this.setData({
  //    pageNum: 1,
  //    list:[],
  //    serachValue:e.detail.value
  //  });
  //   this.getList();    
},
searchChange(e){
  console.log(!e.detail.value)
  if(!e.detail.value){
    this.setData({
      pageNum:1,
      list:[],
      serachValue:''
    });
    this.getList();
  }
},
searchInput(){
  this.selectComponent('#unitSelect').toggle(false);
  this.selectComponent('#moreSelect').toggle(false);
},
// 未走访
visitStatusChange(e){
  this.setData({
    pageNum: 1,
    list:[],
    value1:e.detail
  });
  this.getList();
},


  /**
   * 生命周期函数--监听页面加载
   */


 async parentGZ(e){
    const {value,index}=e.detail;
    let url=value=='1'?'/api/userApi/focusCompany':'/api/userApi/removeFocusCompany';
    let params={};
    if(value=='1'){
      params={
        corpCredit:this.data.list[index].corpKey,
        corpName:this.data.list[index].corpName
      }
    }
    else{
       params={
        corpList:[this.data.list[index].corpKey],
        corpName:this.data.list[index].corpName
      };
    }

   
    const res = await getApp().globalData.request(
      url,
      { ...params },
      "POST"
    );
    if(res.code==200){
      this.setData({
        [`list[${index}].isSubscribed`]:value,
      });
    }
    else{
      wx.showModal({
        title: `操作失败:${res.data}`,
        //content: `操作失败:${res.data}`,
        showCancel:false
      })
    }
   

  },
 // 加载更多
 scrollBottom () {
  this.getList()
},
  onLoad (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
